package org.cncy;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

import java.io.Serializable;

/**
 * 获取应用列表
 */
public class I1001JavaSampler extends AbstractJavaSamplerClient implements Serializable {

    private static final Logger log = LoggingManager.getLoggerForClass();
    private static final long serialVersionUID = 240l;

    public I1001JavaSampler() {
        // log.debug(whoAmI() + "\tConstruct");
    }


    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.setComment("获取应用列表");
        arguments.addArgument("ClientUID", "");
        arguments.addArgument("url", "");
        return arguments;
    }

    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult results = new SampleResult();
        //返回请求值，体现在jmeter查看结果数请求里面
        String ClientUID = javaSamplerContext.getParameter("ClientUID");
        String url = javaSamplerContext.getParameter("url");
        // System.out.println(Utils.url);
        String body = "<?xml version='1.0' encoding='UTF-8' ?><Request><OperID><TenantID>cop</TenantID><ClientUID>" + ClientUID + "</ClientUID><FlowNum>1530904594</FlowNum><Timezone>-1</Timezone><Result>-1</Result><ClientVersion>2.0.148</ClientVersion><ClientBuildNum>200148</ClientBuildNum></OperID><AppInfo><ClientOS>1</ClientOS><Class>0</Class><OSVersion>14</OSVersion><CateId>0</CateId><StartIdx>0</StartIdx><RowNum>500</RowNum></AppInfo></Request>";
        log.debug(body);
        results.setSamplerData("请求地址 : " + url);
        results.setSamplerData(body);
        results.sampleStart();
        try {
            byte[] xmlRequestByte = HttpsXMLUtil.getXMLRequestByte(1001, body);
            // results.setSentBytes(xmlRequestByte.length);
            byte[] bytes = HttpsUtils.doPost(url, xmlRequestByte);

            //  byte[] bytes1 = Utils.interceptBody(bytes);
            results.setSuccessful(true);//这里决定测试用例成功还是失败
            //设置返回信息
            results.setResponseCode("200");
            results.setResponseMessage("成功测试");
            //results.setSampleLabel("获取应用列表");
            //返回结果
            results.setResponseData(new String(Utils.interceptBody(bytes)), "utf-8");

        } catch (Exception e) {
            log.error(e.getMessage());
            results.setSuccessful(false);
        } finally {
            // Record end time and populate the results.
            results.sampleEnd();
        }
        if (log.isDebugEnabled()) {
            log.debug(Utils.whoAmI(this) + "\tTime:\t" + results.getTime());
        }
        return results;
    }
}
